#include "stdint.h"
#include "cpuid.h" 
#include "fpu.h"
#include "optlib.h"
#include "text_video.h"

#include "gmp.h"
#include "oapl.h"

void main(void * mbd, unsigned int magic);

static void OTP_encrypt(const void * data_v, const void * const key_v, const size_t len)
{
    size_t i;
    
    uint8_t * data = (uint8_t *)data_v;
    const uint8_t * const key  = (const uint8_t * const)key_v;
    
    for(i = 0; i < len; i++)
    {
        data[i] = (data[i] ^ key[i]);
    }
}

static void OTP_decrypt(const void * data_v, const void * const key_v, const size_t len)
{
    size_t i;
    
    uint8_t * data = (uint8_t *)data_v;
    const uint8_t * const key  = (const uint8_t * const)key_v;
    
    for(i = 0; i < len; i++)
    {
        data[i] = (data[i] ^ key[i]);
    }
}

void main(void * mbd, unsigned int magic)
{
	parse_cpuid(); 
	
	init_fpu();
	init_sse();

	init_optimized_memory_functions();

	base = (size_t)&kernel_end + 0x1000;

    init_text_video();
    
    printf("Welcome to E/OS\n\n"); 
   
    if(cpuid.sse_max_major)
    {
        printf("SSE v%u.%u detected.\n", cpuid.sse_max_major, cpuid.sse_max_minor);
        printf("Using SSE optimizations...\n");
    }
    else
    {
        WARNING("SSE support was not detected.\n");
        printf("Using standard optimizations...\n");
    }
    
<<<<<<< .mine
    // 0x1040D00C 0x7BFD4C0E

    uint32_t str[4] = {0x12345678, 0x9ABCDEF0, 0x12345678, 0x9ABCDEF0};
    const uint32_t key[4] = {0x12345678, 0x9ABCDEF0, 0x9ABCDEF0, 0x12345678};
    
    unsigned char test_vectors[3][2][32] = 
    {
        {"Key", "Plaintext"},
        {"Wiki", "pedia"},
        {"Secret", "Attack at dawn"}
    };
    
    size_t x;
    for (x = 0; x < 3; x++) {
        size_t y;
        RC4_init(test_vectors[x][0], strlen((char*)test_vectors[x][0]));
    
        printf("0x");
        for (y = 0; y < strlen((char*)test_vectors[x][1]); y++)
            printf("%m", test_vectors[x][1][y] ^ RC4_output());
        printf("\n");
    }

=======
    const char * data = "woottest";
    const char * key = "blehbleh";
    OTP_encrypt(data, key, strlen(key));
    printf("data = \"%s\"\n", data);
    OTP_decrypt(data, key, strlen(key));
    printf("data = \"%s\"\n", data);
    
>>>>>>> .r67
	WARNING("SYS: halting...");
	for(;;){asm("hlt;");}
}








